﻿using DevExpress.Skins;
using DevExpress.UserSkins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace Rail.Start
{
    static class Program
    {
        public static Mutex m;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            try
            {
                //Rail.GISUtilsNTS.PropertiesStructCheck.LayerProperties.TestLyrType();
                //Rail.Business.ReprojectShp.ShpProperties(@"D:\Documents\ArcGIS\Point.shp");
                Commons.LogHelper.Info("系统启动");
                //设置应用程序处理异常方式：ThreadException处理
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                //处理非UI线程异常
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                bool createNew = false;
                using (m = new Mutex(true, Application.ProductName, out createNew))
                {
                    if (createNew)
                    {
                        //皮肤
                        DevExpress.UserSkins.BonusSkins.Register();
                        DevExpress.Skins.SkinManager.EnableFormSkins();

                        m.ReleaseMutex();
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        //Application.Run(new FormMain());

                        ApplicationFacade facade = (ApplicationFacade)ApplicationFacade.Instance;
                        var mf = new FormMain();
                        facade.Startup(mf);
                        Application.Run(mf);
                    }
                    else
                    {
                        DevExpress.XtraEditors.XtraMessageBox.Show("软件正在运行中,请勿重复开启...", "提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
                    }
                }
            }
            catch (Exception e)
            {
                Commons.LogHelper.Error(e);
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Commons.LogHelper.Error(e.ExceptionObject as Exception);
            //UIMessageDialog.ShowErrorDialog(new Form(), (e.ExceptionObject as Exception).Message);
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Commons.LogHelper.Error(e.Exception);
            //UIMessageDialog.ShowErrorDialog(new Form(), e.Exception.Message);
        }
    }
}
